package homewoek1月4号.demo3.d6;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.*;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        List<Dept> dlist = getDepts();
        for (Dept d : dlist) {
            System.out.println(d.getDname()+"  "+d.getDid());
            for (Emp e : d.getElist()) {
                System.out.println(e);
            }
        }
        List<Emp> emp = getTopEmps(getDepts());
        for (Emp e : emp) {
            System.out.println(e.getEname()+"  "+e.getEname()+"  "+e.getDid());
        }
        List<Map<String, String>> list = getEvg(getDepts());
        ArrayList<Map.Entry<String, String>> lsit1 = new ArrayList<>();
        for (Map<String, String> e : list) {
            for (Map.Entry<String, String> entry : e.entrySet()) {
                lsit1.add(entry);
            }
        }
        Collections.sort(lsit1, new Comparator<Map.Entry<String, String>>() {
            @Override
            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                return (int) (Double.parseDouble(o1.getValue())-Double.parseDouble(o2.getValue()));
            }
        });
        System.out.println(lsit1);
        searchEmp("e01");
    }
    public static void searchEmp(String empId) throws DocumentException {
        List<Element> elist = new SAXReader().read("src/czxy/com/demo3/d6/emp.xml").getRootElement().elements();
        HashSet<Emp> set = new HashSet<>();
        for (Element element : elist) {
            set.add(new Emp(element.elementText("emp-name"),element.elementText("emp-id"),element.elementText("dept-id"),element.elementText("emp-salary")));
        }
        for (Emp emp : set) {
            if (emp.getEid().equals(empId)){
                System.out.println(empId+"  "+emp.getEname());
            }
        }
    }
    public static List<Map<String,String>> getEvg(List<Dept> list){
        ArrayList<Map<String, String>> list11 = new ArrayList<>();
        HashMap<String, String> map = new HashMap<>();
        for (Dept dept : list) {
            double d=0.0;
            for (Emp emp : dept.getElist()) {
                d+=Integer.parseInt(emp.getEsalary());
            }
            map.put(dept.getDname(),(d/dept.getElist().size())+"");
        }
        list11.add(map);
        return list11;
    }
    public static List<Emp> getTopEmps(List<Dept> list) throws Exception {
        ArrayList<Emp> emplist = new ArrayList<>();
        int min=1000000;
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.get(i).getElist().size(); j++) {
                if (Integer.parseInt(list.get(i).getElist().get(j).getEsalary())<min){
                    min=Integer.parseInt(list.get(i).getElist().get(j).getEsalary());
                }
            }
        }
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.get(i).getElist().size(); j++) {
                if (Integer.parseInt(list.get(i).getElist().get(j).getEsalary())==min){
                   emplist.add(new Emp(list.get(i).getDname(),list.get(i).getElist().get(j).getEname(),list.get(i).getElist().get(j).getEsalary()));
                }
            }
        }
        return emplist;
    }
    public static List< Dept> getDepts() throws DocumentException {
        HashSet<Emp> set = new HashSet<>();
        ArrayList<Dept> list1 = new ArrayList<>();
        List<Element> dlist = new SAXReader().read("src/czxy/com/demo3/d6/dept.xml").getRootElement().elements();
        List<Element> elist = new SAXReader().read("src/czxy/com/demo3/d6/emp.xml").getRootElement().elements();
        for (Element element : dlist) {
            list1.add(new Dept(element.elementText("dept-name"),element.elementText("dept-id")));
        }
        for (Element element : elist) {
            set.add(new Emp(element.elementText("emp-name"),element.elementText("emp-id"),element.elementText("dept-id"),element.elementText("emp-salary")));
        }
        for (int i = 0; i < list1.size(); i++) {
            for (Emp emp : set) {
                if (list1.get(i).getDid().equals(emp.getDid())){
                    list1.get(i).getElist().add(emp);
                }
            }
        }
        return list1;
    }
}
